package com.etheart.music.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etheart.framework.core.dao.CrudService;
import com.etheart.framework.core.dao.QueryService;
import com.etheart.model.entity.Song;

@Service("music_SongService")
public class SongServiceImp implements SongService {
	@Autowired
	@Qualifier("etheart_QueryService")
	private QueryService queryService;

	@Autowired 
	@Qualifier("etheart_CrudService")
	private CrudService crudService;
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#findAll()
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Song> findAll(){
		return queryService.findAll(Song.class);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#addSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void addSong(Song Song){
		crudService.create(Song);
	}
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#updateSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void updateSong(Song Song){
		crudService.update(Song);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#deleteSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void deleteSong(Song Song){
		crudService.delete(Song);
	}

	@Override
	public Song findById(Long id) {
		return queryService.findById(Song.class, id);
	}

	@Override
	public Song findByNetId(String netId) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Song.class).add(Restrictions.eq("netId", netId+""));
		List<Song> songs=queryService.findListByDetachedCriteria(criteria);
		if (songs.size()>0) {
			return songs.get(0);
		}
		return null;
	}

	@Override
	public String parseLrcContent(Song song) {
		return song.getLrcContent().replaceAll("\r\n", "<br/>");
	}


	
}
